﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace PHP
{
	public partial class main : Form
	{
		public main()
		{
			InitializeComponent();
		}
		List<ClassModle> ds;
		private FormSetingStore fs;
		private void main_Load(object sender, EventArgs e)
		{
			fs = new FormSetingStore(@"SOFTWARE\PHP\Form\1.0", this.Name);
			fs.ApplySetings(this);

			txtNameData.Focus();

			btAction.Enabled = false;
			btSave.Enabled = false;

		}
		private MySqlConnection conn = null;
		private MySqlConnection kn()
		{
			if (conn.State != ConnectionState.Open)
				conn.Open();
			return conn;
		}
		private List<string> getTableName()
		{

			List<string> l = new List<string>();
			StringBuilder st = new StringBuilder();
			try
			{
				Console.WriteLine("Connecting to MySQL...");
				//	MessageBox.Show("Connecting to MySQL...");
				string sql = @"SHOW FULL TABLES IN " + txtNameData.Text.Trim();




				MySqlCommand cmd = new MySqlCommand(sql, kn());
				MySqlDataReader rdr = cmd.ExecuteReader();

				while (rdr.Read())
				{
					//	Console.WriteLine(rdr[0]);
					string str = rdr[0].ToString();
					l.Add(str);
					// + " -- " + rdr[1]);
				}
				rdr.Close();
				kn().Close();

				// Perform database operations
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				//	MessageBox.Show(ex.Message.ToString());
			}
			//MessageBox.Show("Done");
			Console.WriteLine("Done.");
			return l;
		}
		private List<Table> getTable()
		{
			List<Table> ltb = new List<Table>();

			foreach (var item in getTableName())
			{
				//lay ten cot va kieu du lieu
				string sql = @"SHOW COLUMNS FROM " + item.ToString();

				MySqlCommand cmd = new MySqlCommand(sql, kn());
				MySqlDataReader rdr = cmd.ExecuteReader();
				Table tb = new Table();
				tb.nameTable = item.ToString();
				while (rdr.Read())
				{
					//	Console.WriteLine(rdr[0]);

					tb.nameColumn.Add(rdr[0].ToString());
					tb.typeOfCoumn.Add(rdr[1].ToString());
					//	string str = rdr[0].ToString();
					// + " -- " + rdr[1]);
				}
				ltb.Add(tb);

				rdr.Close();
			}

			return ltb;

		}

		private void dg1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			int row = dg1.CurrentCell.RowIndex;

			if (dg1[0, row].Value != null)
			{
				txt1.Text = ds[row].KQ.ToString();
			}

		}

		private void btAction_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < ds.Count; i++)
			{
				ds[i].nameTable = dg1[0, i].Value.ToString();
				ds[i].taoClass();

			}
			btSave.Enabled = true;
		//	btAction.Enabled = false;
		}

		private void main_FormClosing(object sender, FormClosingEventArgs e)
		{
			fs.SaveSettings(this);

		}

		private void btLoadTable_Click(object sender, EventArgs e)
		{
			string connStr = "server=localhost;user=root;database=" + txtNameData.Text.Trim() + ";port=3306;password=\"\";";

			conn = new MySqlConnection(connStr);

			List<Table> dsb = getTable();
			ds = new List<ClassModle>();
			int i = 0;
			foreach (var item in dsb)
			{
				ClassModle tmp = new ClassModle(item);
				ds.Add(tmp);
				dg1.Rows.Add();
				dg1[0, i++].Value = item.nameTable;
			}
			btLoadTable.Enabled = false;
			btAction.Enabled = true;

		}

		private void btSave_Click(object sender, EventArgs e)
		{
			readWrite r = new readWrite();

			for (int i = 0; i < dg1.RowCount -1; i++)
			{
				List<string> st = new List<string>();
				st.Add(ds[i].KQ.ToString());
				r.writeData(txLocal.Text.Trim()+"\\"+ dg1[0, i].Value.ToString() + ".php", st);
			}
		}
	}
}